//
//  PubViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/14.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "PubViewController.h"
#import "JourneyModel.h"
#import "journeyCollectionTableCell.h"
#import "PubCityViewController.h"
#import "ChoosePubViewController.h"

@interface PubViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *hotCityArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UIButton *cityButton;
@property (nonatomic, strong) UIButton *pubButton;
@property (nonatomic, copy) NSString *strId;
@property (nonatomic, copy) NSString *strName;
@property (nonatomic, strong) UIAlertController *alert;
@property (nonatomic, strong) UIAlertView *alertView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIView *viewLine;

@end

@implementation PubViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    }
    return self;
}

#pragma mark 订酒店数据解析
- (void)createData {
    NSString *strUrl = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=9&lat=38.8826458947218&lon=121.5397497056552&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%25208.1.2&track_user_id=6654242&v=1%20";
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *strDic = result;
        NSMutableArray *strArr = strDic[@"data"];
        self.hotCityArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in strArr) {
            JourneyModel *journeyModel = [[JourneyModel alloc] init];
            [journeyModel setValuesForKeysWithDictionary:dic];
            [self.hotCityArr addObject:journeyModel];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"错误是:%@",error);
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.text = @"订酒店";
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    
    [self createTableView];
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView的创建
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 554)];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = 500 * HEIGHT;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
    // 判断系统的版本是否大于等于8.0
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        // 提示框
        self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择城市" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [self.alert addAction:action];
    }
    else {
        self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择城市" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    }
    
    [self createHeadView];
}

#pragma mark 头视图
- (void)createHeadView {
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 330)];
    self.headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = self.headView;
    
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 300)];
    [self.headView addSubview:self.headImageView];
    self.headImageView.userInteractionEnabled = YES;
    self.headImageView.image = [UIImage imageNamed:@"酒店"];

    self.cityButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.cityButton.frame = CGRectMake(WIDTH * 50, HEIGHT * 100, WIDTH * 180, HEIGHT * 30);
    self.cityButton.backgroundColor = [UIColor colorWithRed:0.738 green:1.000 blue:0.886 alpha:0.5];
    [self.headImageView addSubview:self.cityButton];
    [self.cityButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.cityButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cityButton setTitle:@"选择城市" forState:(UIControlStateNormal)];

    self.pubButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.pubButton.frame = CGRectMake(WIDTH * 230, HEIGHT * 200, WIDTH * 100, HEIGHT * 30);
    self.pubButton.backgroundColor = [UIColor colorWithRed:0.738 green:1.000 blue:0.886 alpha:0.5];
    [self.headImageView addSubview:self.pubButton];
    [self.pubButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.pubButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.pubButton setTitle:@"选择酒店" forState:(UIControlStateNormal)];
    
    self.topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 * HEIGHT, WIDTH * 375, HEIGHT * 29)];
    [self.headView addSubview:self.topLabel];
    self.topLabel.backgroundColor = [UIColor whiteColor];
    self.topLabel.font = [UIFont boldSystemFontOfSize:20];
    self.topLabel.text = @"    热门城市酒店";
    
    self.viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 329 * HEIGHT, 375 * HEIGHT, 1 * HEIGHT)];
    [self.headView addSubview:self.viewLine];
    self.viewLine.backgroundColor = [UIColor blackColor];
    self.viewLine.alpha = 0.3;
    
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.tableView);
        make.height.equalTo(@(330 * HEIGHT));
        make.width.equalTo(self.tableView.mas_width);
    }];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.headView);
        make.height.equalTo(@(HEIGHT * 300));
    }];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.headView);
        make.height.equalTo(@(29 * HEIGHT));
        make.top.equalTo(self.headImageView.mas_bottom);
    }];
    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.headView);
        make.top.equalTo(self.topLabel.mas_bottom);
        make.bottom.equalTo(self.headView.mas_bottom);
    }];
    [self.cityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_left).offset(50 * WIDTH);
        make.width.equalTo(self.headImageView.mas_width).multipliedBy(0.48);
        make.top.equalTo(self.headImageView.mas_top).offset(100 * HEIGHT);
        make.height.equalTo(@(30 * HEIGHT));
    }];
    [self.pubButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cityButton.mas_right);
        make.width.equalTo(self.headImageView.mas_width).multipliedBy(0.27);
        make.top.equalTo(self.headImageView.mas_top).offset(200 * HEIGHT);
        make.height.equalTo(@(30 * HEIGHT));
    }];
}

#pragma mark 点击方法
- (void)buttonClick:(UIButton *)button {
    if (button == self.cityButton) {
        PubCityViewController *pubCityVC = [[PubCityViewController alloc] init];
        [self.navigationController pushViewController:pubCityVC animated:YES];
        
        void(^block)(NSArray *) = ^(NSArray *arr){
            [self.headImageView sd_setImageWithURL:[NSURL URLWithString:arr[1]] placeholderImage:[UIImage imageNamed:@"占位图"]];
            [self.cityButton setTitle:arr[2] forState:(UIControlStateNormal)];
            self.strId = arr[0];
            self.strName = arr[2];
        };
        pubCityVC.blockOnce = block;
    }
    else if (button == self.pubButton) {
        if (self.strId == nil) {
            [self presentViewController:self.alert animated:YES completion:nil];
            [self.alertView show];
        }
        else{
            ChoosePubViewController *choosePubVC = [[ChoosePubViewController alloc] init];
            [self.navigationController pushViewController:choosePubVC animated:YES];
            choosePubVC.strId = self.strId;
            choosePubVC.strName = self.strName;
        }
    }
}

#pragma mark tableView的数据处理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    journeyCollectionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[journeyCollectionTableCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.hotCityArr.count > indexPath.row) {
        cell.arr = self.hotCityArr;
        [cell.journeyCollectionView reloadData];
        void(^block)(NSDictionary *) = ^(NSDictionary *dic) {
            ChoosePubViewController *choosePubVC = [[ChoosePubViewController alloc] init];
            [self.navigationController pushViewController:choosePubVC animated:YES];
            choosePubVC.strId = dic[@"id"];
            choosePubVC.strName = dic[@"name"];
        };
        cell.block = block;
    }
    return cell;
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
        self.viewLine.backgroundColor = [UIColor whiteColor];
        self.topLabel.textColor = [UIColor whiteColor];
        self.topLabel.backgroundColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.viewLine.backgroundColor = [UIColor blackColor];
        self.topLabel.textColor = [UIColor blackColor];
        self.topLabel.backgroundColor = [UIColor whiteColor];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
